<?php
/**
 * Created by PhpStorm.
 * User: TheLuther
 * Date: 2019/8/29
 * Time: 21:06
 */

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\FormField as FieldModel;
use app\common\model\Form as FormModel;
use think\facade\Request;


class Field extends AdminBase
{
    public function index()
    {
        $formid = Request::param('formid/s');
        $model = FormModel::where(['guid'=>$formid])->findOrEmpty();
        if(!$model){
            $this->error('参数错误');
        }
        if(Request::isAjax()){
            $list = FieldModel::instance()->where(['formid'=>$formid])->order('sort desc,addtime desc')->select();
            //$list = FieldModel::instance()->order('sort desc,edittime desc')->select();
            $this->success('数据列表','',$list);
        }else{
            $this->assign('formid',$formid);
            return $this->fetch();
        }
    }

    public function add(){
        $formid = Request::param('formid/s');

        if(Request::isPost()){
            $formdata = Request::param('form/a');
            $time = time();
            $formdata['guid'] = guid();
            $formdata['formid'] = $formid;
            $formdata['addtime'] = $time;
            $formdata['edittime'] = $time;
            $formdata['configs'] = json_encode(isset($formdata['configs'])?$formdata['configs']:'');
            $res = FieldModel::instance()->add($formdata);
            if( $res === true ){
                $this->success('成功',url('index').'?formid='.$formid);
            }
            else{
                $this->error($res);
            }
        }else{
            $model = array();
            $model['type'] = '';
            $model['pattern'] = '';
            $model['errortip'] = '';
            $model['sql'] = '';
            $model['show'] = true;
            $model['status'] = true;
            $model['showintable'] = true;
            $this->assign('model',$model);
            $this->assign('formid',$formid);
            $this->assign('type',FieldModel::instance()->type);
            $this->assign('pattern',FieldModel::instance()->pattern);
            $this->assign('sql',FieldModel::instance()->createsql);
            return $this->fetch('field');
        }
    }

    public function edit(){
        $fieldid = Request::param('fieldid/s');
        $model = FieldModel::instance()->where(['guid'=>$fieldid])->findOrEmpty();
        if(!$model)
            $this->error('参数错误');

        $formid = $model['formid'];
        if(Request::isPost()){
            $formdata = Request::param('form/a');
            $time = time();
            $formdata['formid'] = $formid;
            $formdata['edittime'] = $time;
            $formdata['configs'] = json_encode(isset($formdata['configs'])?$formdata['configs']:'');
            $res = FieldModel::instance()->edit($formdata);
            if( $res === true ){
                $this->success('成功',url('index').'?formid='.$formid);
            }
            else{
                $this->error($res);
            }
        }else{
            $model['configs'] = json_decode($model['configs'],true);
            $this->assign('model',$model);
            $this->assign('formid',$formid);
            $this->assign('type',FieldModel::instance()->type);
            $this->assign('pattern',FieldModel::instance()->pattern);
            $this->assign('sql',FieldModel::instance()->createsql);
            return $this->fetch('field');
        }
    }

    public function remove(){
        $fieldid = Request::param('fieldid/s');
        $res = FieldModel::instance()->remove(['guid'=>$fieldid]);
        if($res === true)
        {
            $this->success('成功');
        }else{
            $this->error($res);
        }
    }

}